﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Cooperation.Layout;
using Cooperation.Common;
using System.Text.RegularExpressions;
using System.Threading;
using System.Net;

namespace Cooperation
{
    public partial class Index : Form
    {
        private Account UserAccountInfo = null;
        public static Index IndexForm { get; private set; }
        public Index()
        {
            InitializeComponent();
            LoadAccountSetting();
            SetAccountSetting();
            this.SetBounds((Screen.GetBounds(this).Width / 2) - (this.Width / 2),
                (Screen.GetBounds(this).Height / 2) - (this.Height / 2),
                this.Width, this.Height, BoundsSpecified.Location);
            Window.AnimateWindow(Handle, 500, Window.AW_BLEND);
            IndexForm = this;
        }
        private void Form_Load(object sender, EventArgs e)
        {
            if (UserAccountInfo.AutoLogin)
            {
                if (UserAccountInfo.LastLogin.AddMonths(1) < DateTime.Now)
                {
                    MessageBox.Show("您上次登录是一个月以前，请通过登录名密码登录");
                    return;
                }
                Login();
            }
        }
        private void LoadAccountSetting()
        {
            if (System.IO.File.Exists("coop_u_dx"))
            {
                using (System.IO.StreamReader sr = new System.IO.StreamReader("coop_u_dx"))
                {
                    string code = sr.ReadToEnd();
                    code = AesAlgorithm.Decrypt(code);
                    if (!String.IsNullOrEmpty(code))
                    {
                        Account account = StringPlug.ToObject<Account>(code);
                        if (account != null)
                        {
                            UserAccountInfo = account;
                            return;
                        }
                    }
                }
            }
            UserAccountInfo = new Account()
            {
                AutoLogin = false,
                RememberMe = true
            };
        }
        private void SetAccountSetting()
        {
            if (UserAccountInfo != null)
            {
                this.ckbautologin.Checked = UserAccountInfo.AutoLogin;
                this.ckbremember.Checked = UserAccountInfo.RememberMe;
                if (this.ckbremember.Checked)
                {
                    this.txtUserName.Text = UserAccountInfo.Username;
                    this.txtPassword.Text = UserAccountInfo.Password;
                }
            }
        }
        private void Login()
        {
            string username = this.txtUserName.Text;
            string password = this.txtPassword.Text;
            this.Visible = false;
            Cooperation.Login.ShowLoginForm(username, password);
            try
            {
                if (Cooperation.Login.LoginForm.DoLogin())
                {
                    if (this.ckbremember.Checked)
                    {
                        using (System.IO.StreamWriter sw = new System.IO.StreamWriter("coop_u_dx", false))
                        {
                            string code = StringPlug.ToJson(
                                new Account()
                                {
                                    AutoLogin = this.ckbautologin.Checked,
                                    LastLogin = DateTime.Now,
                                    Password = this.txtPassword.Text,
                                    RememberMe = this.ckbremember.Checked,
                                    Username = this.txtUserName.Text
                                });
                            code = AesAlgorithm.Encrypt(code);
                            sw.Write(code);
                            sw.Flush();
                        }
                    }
                    Cooperation.Login.LoginForm.Visible = false;
                    Home.ShowHomeForm();
                }
                else
                {
                    this.txtPassword.Text = "";
                    Cooperation.Login.LoginForm.Visible = false;
                    LoginFaild.ShowLoginFaildForm();
                }
            }
            catch (WebException)
            {
                MessageBox.Show("连接失败，请检查您的网络连接");
            }
        }
        private void Form_FormClosing(object sender, FormClosingEventArgs e)
        {
            Window.AnimateWindow(this.Handle, 500, Window.AW_SLIDE | Window.AW_HIDE | Window.AW_BLEND);
            Application.Exit();
        }
        private void button2_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("http://www.yaojianzhi.com/account/bregister");
        }
        private void button1_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(this.txtUserName.Text) ||
                String.IsNullOrEmpty(this.txtPassword.Text))
            {
                MessageBox.Show("请输入用户名和密码");
                return;
            }
            if (!new Regex("^\\w+((-\\w+)|(\\.\\w+))*\\@[A-Za-z0-9]+((\\.|-)[A-Za-z0-9]+)*\\.[A-Za-z0-9]+$").IsMatch(this.txtUserName.Text))
            {
                MessageBox.Show("用户名格式错误，应该为电子邮箱格式");
                return;
            }
            Login();
        }
    }

}
